#include "timer.h"
#include "isr.h"
#include "print.h"
#include "task.h"

unsigned int timer_ticks = 0;

static void timer_handler(regs r)
{
	timer_ticks++;
	switch_task();
}

void timerInstall(int freq)
{
	register_interrupt_handler(IRQ0, &timer_handler);
	
	unsigned int div = 1193180 / freq;
	
	// command byte
	outportb(0x43, 0x36);
	
	// send frequency
	outportb(0x40, (unsigned char)(div & 0xFF));
	outportb(0x40, (unsigned char)((div >> 8) & 0xFF));
}

